import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import {uuid} from "@/utils";

Vue.config.productionTip = false

import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import GoEasy from 'goeasy';
const goEasy = GoEasy.getInstance({
    host:'hangzhou.goeasy.io', //新加坡host：singapore.goeasy.io
    appkey: "BC-db7034fe57404f1d9f750f942a0a312b", //替换为您的应用appkey
    modules: ['pubsub']
});

// 建议在main.js里初始化全局的GoEasy对象
Vue.prototype.goEasy = goEasy;







Vue.use(ElementUI);
Vue.config.productionTip = false

axios.defaults.baseURL = "http://192.168.137.196:8888/"
axios.defaults.withCredentials=true;
Vue.prototype.axios = axios;

let uid=uuid(); //获取uuid
if(sessionStorage.getItem('uid')==null){ //如果sessionStorage没有则存储uuid
  sessionStorage.setItem('uid',uid);
}

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token= sessionStorage.getItem("token");
  config.headers.token=token;
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});


// // 导航守卫-- 动态路由
router.beforeEach((to, from, next) => {
  // let isAuthenticated = sessionStorage.getItem("token");//获取token

//   if (to.name !== 'login' && !isAuthenticated) {//如果没有登录且目标路由不是登录则导航到登录页面
//     next({name: 'login'})//
//   }
  //如果已经登录
  // if (isAuthenticated && to.name !== 'login' && router.getRoutes().length == 2) {//index.js 默认配置了两个静态理由 ,则需要执行动态路由
    axios.get('http://192.168.137.1:8888/system/menu/list').then(res => {
      let menuData = res.data.data;//获取一级菜单
      menuData.forEach(menu => {
            // eslint-disable-next-line no-prototype-builtins
            if (menu.hasOwnProperty("children")) {
              menu.children.forEach(subMenu => { //遍历二级菜单
                let newRouter = {
                  name: subMenu.name,
                  path: subMenu.path
                }
                newRouter.component = () => import( './views/' + subMenu.component + '.vue');
                router.options.routes[0].children.push(newRouter);
              })
            }
          }
      )
      router.addRoutes(router.options.routes);
    })

  // }
//

  next();

})




new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
